package com.ouc.dataMange.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;

import com.ouc.common.config.OUCAjaxResult;
import com.ouc.common.utils.file.FileUtils;
import com.ouc.dataMange.domain.FileTreeList;
import com.ouc.dataMange.domain.SysDataSet;
import com.ouc.dataMange.service.ISysDataSetService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ouc.common.annotation.Log;
import com.ouc.common.core.controller.BaseController;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.enums.BusinessType;
import com.ouc.dataMange.domain.SysMemoryDevice;
import com.ouc.dataMange.service.ISysMemoryDeviceService;
import com.ouc.common.utils.poi.ExcelUtil;
import com.ouc.common.core.page.TableDataInfo;

/**
 * memoryDeviceController
 *
 * @author yuhao
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/dataMange/memoryDevice")
public class SysMemoryDeviceController extends BaseController {
  @Autowired
  private ISysMemoryDeviceService sysMemoryDeviceService;

  @Autowired
  private ISysDataSetService sysDataSetService;

  /**
   * 查询memoryDevice列表
   */
//  @PreAuthorize("@ss.hasPermi('dataMange:memoryDevice:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysMemoryDevice sysMemoryDevice) {
    startPage();
    List<SysMemoryDevice> list = sysMemoryDeviceService.selectSysMemoryDeviceList(sysMemoryDevice);
//    FileSystemView fsv = FileSystemView.getFileSystemView();
    for (int i = 0; i < list.size(); i++) {
      String sysMemoryDevice1 = list.get(i).getSysMemoryDevice();
      File file = new File(sysMemoryDevice1);
      list.get(i).setDiskTotalSpace(file.getTotalSpace() / (1024 * 1024 * 1024));
      list.get(i).setDiskFreeSpace(file.getFreeSpace() / (1024 * 1024 * 1024));
      list.get(i).setDiskUseSpace((file.getTotalSpace() - file.getFreeSpace()) / (1024 * 1024 * 1024));
//      list.get(i).setDiskUseSpace(FileUtils.getFilesLength(file) / (1024 * 1024 * 1024));
    }
    return getDataTable(list);
  }

  /**
   * 导出memoryDevice列表
   */
  @PreAuthorize("@ss.hasPermi('dataMange:memoryDevice:export')")
  @Log(title = "memoryDevice", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysMemoryDevice sysMemoryDevice) {
    List<SysMemoryDevice> list = sysMemoryDeviceService.selectSysMemoryDeviceList(sysMemoryDevice);
    ExcelUtil<SysMemoryDevice> util = new ExcelUtil<SysMemoryDevice>(SysMemoryDevice.class);
    util.exportExcel(response, list, "memoryDevice数据");
  }

  /**
   * 获取memoryDevice详细信息
   */
  @PreAuthorize("@ss.hasPermi('dataMange:memoryDevice:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Integer id) {
    return success(sysMemoryDeviceService.selectSysMemoryDeviceById(id));
  }

  /**
   * 新增memoryDevice
   */
  @PreAuthorize("@ss.hasPermi('dataMange:memoryDevice:add')")
  @Log(title = "memoryDevice", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysMemoryDevice sysMemoryDevice) {
    return toAjax(sysMemoryDeviceService.insertSysMemoryDevice(sysMemoryDevice));
  }

  /**
   * 修改memoryDevice
   */
  @PreAuthorize("@ss.hasPermi('dataMange:memoryDevice:edit')")
  @Log(title = "memoryDevice", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysMemoryDevice sysMemoryDevice) {
    return toAjax(sysMemoryDeviceService.updateSysMemoryDevice(sysMemoryDevice));
  }

  /**
   * 删除memoryDevice
   */
  @PreAuthorize("@ss.hasPermi('dataMange:memoryDevice:remove')")
  @Log(title = "memoryDevice", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Integer[] ids) {
    return toAjax(sysMemoryDeviceService.deleteSysMemoryDeviceByIds(ids));
  }

  /**
   * 根据数据集id，查询存储空间列表及所用空间情况
   *
   */
  @ResponseBody
  @RequestMapping("/getMemoryListBySetId")
  public OUCAjaxResult getMemoryListBySetId(@RequestParam(value = "setId") Integer setId){
    List<SysMemoryDevice> memoryDeviceListResult = new ArrayList<>();
    SysMemoryDevice sysMemoryDevice = new SysMemoryDevice();
    sysMemoryDevice.setDataSetId(setId);
    List<SysMemoryDevice> memoryDeviceList = sysMemoryDeviceService.selectSysMemoryDeviceList(sysMemoryDevice);
    for (int i = 0; i < memoryDeviceList.size(); i++) {
      SysMemoryDevice sysMemoryDevice1 = memoryDeviceList.get(i);
      File file = new File(sysMemoryDevice1.getSysMemoryDevice());
      sysMemoryDevice1.setDiskTotalSpace(file.getTotalSpace() / (1024 * 1024 * 1024));
      sysMemoryDevice1.setDiskFreeSpace(file.getFreeSpace() / (1024 * 1024 * 1024));
      sysMemoryDevice1.setDiskUseSpace((file.getTotalSpace() - file.getFreeSpace()) / (1024 * 1024 * 1024));
      memoryDeviceListResult.add(sysMemoryDevice1);
    }
    return OUCAjaxResult.ok(memoryDeviceListResult);
  }


  /**
   * 获取存储文件的树状列表
   * sysMemoryDevice-sysDatamange
   */
  @RequestMapping("/getFileTreeList")
  @ResponseBody
  public OUCAjaxResult getFileTreeList(SysDataSet sysDataSet) {
    List<FileTreeList> fileTreeList = new ArrayList<>();
    List<SysDataSet> dataSetList = sysDataSetService.selectSysDataSetList(sysDataSet);
    for (int i = 0; i < dataSetList.size(); i++) {
      FileTreeList fileTree = new FileTreeList();
      fileTree.setNodeId("0");
      fileTree.setDataSetId(dataSetList.get(i).getId());
      fileTree.setSysName(dataSetList.get(i).getDataSetName());
      fileTree.setSysDevice(dataSetList.get(i).getDataSetDevice());

      SysMemoryDevice sysMemoryDeviceSelect = new SysMemoryDevice();
      sysMemoryDeviceSelect.setDataSetId(dataSetList.get(i).getId());
      List<SysMemoryDevice> memoryDeviceList = sysMemoryDeviceService.selectSysMemoryDeviceList(sysMemoryDeviceSelect);
      List<FileTreeList> fileTreeListChildren = new ArrayList<>();
      for (int j = 0; j < memoryDeviceList.size(); j++) {
        FileTreeList fileTree1 = new FileTreeList();
        fileTree1.setNodeId("1");
        fileTree1.setDataSetId(memoryDeviceList.get(j).getDataSetId());
        fileTree1.setDataMemoryId(memoryDeviceList.get(j).getId());
        fileTree1.setSysName(memoryDeviceList.get(j).getSysMemoryIdfo());
        fileTree1.setSysDevice(memoryDeviceList.get(j).getSysMemoryDevice());

        String sysMemoryDevice1 = memoryDeviceList.get(j).getSysMemoryDevice();
        File file = new File(sysMemoryDevice1);
        fileTree1.setDiskTotalSpace(file.getTotalSpace() / (1024 * 1024 * 1024));
        fileTree1.setDiskFreeSpace(file.getFreeSpace() / (1024 * 1024 * 1024));
        fileTree1.setDiskUseSpace((file.getTotalSpace() - file.getFreeSpace()) / (1024 * 1024 * 1024));

        fileTreeListChildren.add(fileTree1);
      }
      fileTree.setChildren(fileTreeListChildren);
      fileTreeList.add(fileTree);
    }


//    List<SysMemoryDevice> memorylist = sysMemoryDeviceService.selectSysMemoryDeviceList(sysMemoryDevice);
//    for (int i = 0; i < memorylist.size(); i++) {
//      FileTreeList fileTree = new FileTreeList();
////      fileTree.setSysMemoryDevice(memorylist.get(i));
//      fileTree.setNodeId("0");
//      fileTree.setDataMemoryId(memorylist.get(i).getId());
//      fileTree.setSysName(memorylist.get(i).getSysMemoryIdfo());
//      fileTree.setSysDevice(memorylist.get(i).getSysMemoryDevice());
//
//
//      SysDataSet sysDataSetSelect = new SysDataSet();
//      sysDataSetSelect.setDataMemoryId(memorylist.get(i).getId());
//      List<SysDataSet> sysDataSets = sysDataSetService.selectSysDataSetList(sysDataSetSelect);
//      List<FileTreeList> fileTreeListChildren = new ArrayList<>();
//      for (int j = 0; j < sysDataSets.size(); j++) {
//        FileTreeList fileTree1 = new FileTreeList();
//        fileTree1.setNodeId("1");
//        fileTree1.setDataSetId(sysDataSets.get(j).getId());
//        fileTree1.setDataMemoryId(sysDataSets.get(j).getDataMemoryId());
//        fileTree1.setSysName(sysDataSets.get(j).getDataSetName());
//        fileTree1.setSysDevice(sysDataSets.get(j).getDataSetDevice());
//        fileTreeListChildren.add(fileTree1);
//      }
//      fileTree.setChildren(fileTreeListChildren);
//      fileTreeList.add(fileTree);
//    }
    return OUCAjaxResult.ok(fileTreeList);
  }
}
